package com.caifu.service.impl;

import com.caifu.mapper.UtilMapper;
import com.caifu.service.UtilService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Lyf
 * @Date: 2020/8/12 09:53
 * @Description:
 */
@Service
public class UtilServiceImpl implements UtilService {

    @Autowired
    private UtilMapper utilMapper;

    @Override
    @Transactional
    public List<String> getJurisdiction(int userNo) throws Exception {
        try {
            List<String> result = new ArrayList<>();
            List<Map<String,String>> list = utilMapper.getJurisdiction(userNo);
            if(list != null && list.size() > 0){
                Map<String,String> map = new HashMap<>();
                for(Map<String,String> m : list){
                    List<Map<String,String>> jList = utilMapper.getChildNodeByParentNode(Integer.parseInt(String.valueOf(m.get("org_id"))));
                    if(jList != null && jList.size() > 0){
                        for(Map<String,String> j : jList){
                            String id = String.valueOf(j.get("org_id"));
                            if(StringUtils.isBlank(map.get(id))){
                                map.put(id,id);
                            }
                        }
                    }
                }
                if(map != null && map.size() > 0){
                    for(String value : map.values()){
                       result.add(value);
                    }
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("查询失败！");
        }
    }

    @Override
    @Transactional
    public List<Map<String, String>> getJurisdictionByParentNo(int orgId) throws Exception {
        return utilMapper.getChildNodeByParentNode(orgId);
    }

    @Override
    @Transactional
    public List<Map<String,String>> getJurisdictionByChildrenNo(int orgId) throws Exception {
        return utilMapper.getChildNodeByChildrenNode(orgId);
    }

    @Override
    @Transactional
    public List<Map<String, String>> getUserList(int start, int pageSize, String userName,String userPid, List<String> orgIds) {
        return utilMapper.getUserList(start,pageSize,userName,userPid,orgIds);
    }

    @Override
    public int getUserListCount(String userName,String userPid, List<String> orgIds) {
        return utilMapper.getUserListCount(userName,userPid,orgIds);
    }
}
